/*
 * @(#)MaintMtmWorkflowStepPriorityRuleAction.java
 *
 * Copyright (c) 2003 DCIVision Ltd
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of DCIVision
 * Ltd ("Confidential Information").  You shall not disclose such Confidential
 * Information and shall use it only in accordance with the terms of the license
 * agreement you entered into with DCIVision Ltd.
 */
package com.dcivision.workflow.web;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.dcivision.framework.ApplicationException;
import com.dcivision.framework.ErrorConstant;
import com.dcivision.framework.GlobalConstant;
import com.dcivision.framework.SessionContainer;
import com.dcivision.framework.SystemFunctionConstant;
import com.dcivision.framework.TextUtility;
import com.dcivision.framework.Utility;
import com.dcivision.framework.web.AbstractActionForm;

/**
 * MaintMtmWorkflowStepPriorityRuleAction.java
 * @author      Tony Chen
 * @company     DCIVision Limited
 * @creation date   23/11/2004
 * @version     $Revision: 1.1 $
 */


public class MaintMtmWorkflowStepPriorityRuleAction extends com.dcivision.framework.web.AbstractMaintAction {

  public static final String REVISION = "$Revision: 1.1 $";

  
  /** Creates a new instance of MaintMtmWorkflowStepPriorityRuleAction */
  public MaintMtmWorkflowStepPriorityRuleAction() {
    super();
  }

  /** getFunctionCode
   *
   * Abstract function which sub-class should implement to return the corresponding
   * function code.
   *
   * @return   The function code
   */
  public String getFunctionCode() {
    return SystemFunctionConstant.WORKFLOW_RECORD;
  }

  /** getMajorDAOClassName
   *
   * Abstract function which sub-class should implement to return the corresponding
   * major DAO class name used in this class.
   *
   * @return   The DAO class name
   */
  public String getMajorDAOClassName() {
    return "com.dcivision.workflow.dao.MtmWorkflowProgressPriorityRuleDAObject";
  }

  public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws ServletException {
    AbstractActionForm maintForm = (AbstractActionForm)form;

    String opMode = (maintForm != null) ? maintForm.getOpMode() : request.getParameter("opMode"); //maintForm.getOpMode();
    String navMode = (maintForm != null) ? maintForm.getNavMode() : request.getParameter("navMode"); //maintForm.getNavMode();

    if (GlobalConstant.OP_MODE_REFRESH.equals(opMode)) {
      // Set the function code for current action.
      ActionForward forward = this.retrieveFunctionCode(request, response, mapping);
      if (forward != null) {
        return forward;
      }
      refreshFormData(mapping, maintForm, request, response);
      return mapping.findForward(GlobalConstant.NAV_MODE_REFRESH);
    } else {
      return super.execute(mapping, form, request, response);
    }
  }

  public void refreshFormData(ActionMapping mapping, AbstractActionForm form, HttpServletRequest request, HttpServletResponse response) throws
    ApplicationException {
    List resultList = new ArrayList();

    try {
      SessionContainer sessionContainer = this.getSessionContainer(request);
      MaintMtmWorkflowStepPriorityRuleForm priorityRuleForm = (MaintMtmWorkflowStepPriorityRuleForm)form;
      
      String priorityRuleListValue = "";
      String priorityRule = "";
      String[] tmpRuleArr = null;
      priorityRule = priorityRuleForm.getEscalateAtAmt() + "," +
            priorityRuleForm.getEscalateAtField() + "," +
            priorityRuleForm.getEscalateTrigger() + "," +         
            priorityRuleForm.getEscalateLevel() + "," + 
            priorityRuleForm.getEscalateAckMethodByEmail() + "," +
            priorityRuleForm.getEscalateAckMethodBySystem() ;
      
      if(!Utility.isEmpty(request.getParameter("priorityRuleListValue")) && !"null".equals(request.getParameter("priorityRuleListValue"))){
        priorityRuleListValue = request.getParameter("priorityRuleListValue");
      }
      
      if(!Utility.isEmpty(priorityRuleListValue)){
        tmpRuleArr = TextUtility.splitString(priorityRuleListValue,"|");
      }
      
      String[] deleteItemArr = request.getParameterValues("deleteItem");
      boolean checked = false;
      priorityRuleListValue = "";
      
      for(int j=0; !Utility.isEmpty(tmpRuleArr) &&  j<tmpRuleArr.length; j++){
        checked = false;
        for(int i=0; !Utility.isEmpty(deleteItemArr) && i<deleteItemArr.length; i++){
          if(j == Integer.parseInt(deleteItemArr[i])){
            checked = true;
            break;
          }    
        }
        if(!checked){          
          if(Utility.isEmpty(priorityRuleListValue)){
            priorityRuleListValue += tmpRuleArr[j];  
          }else{
            priorityRuleListValue += "|" + tmpRuleArr[j];
          }
        }
      }
      
      if(Utility.isEmpty(priorityRuleListValue)){
        priorityRuleListValue = priorityRule;
      } else {
        priorityRuleListValue += "|" + priorityRule;  
      }
      
      request.setAttribute("priorityRuleListValue", priorityRuleListValue);
    
    } catch (Exception e) {
      log.error(e, e);
      throw new ApplicationException(ErrorConstant.COMMON_FATAL_ERROR, e);
    } finally {
    }
  }

  
}
